package com.miyake.loveviewweb.client.explorer;

import com.google.appengine.api.datastore.Key;
import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.Tree;
import com.miyake.loveviewweb.client.DataServiceListener;
import com.miyake.loveviewweb.client.DataServiceWrapper;
import com.miyake.loveviewweb.shared.model.ChartModel;
import com.miyake.loveviewweb.shared.model.DataModel;
import com.miyake.loveviewweb.shared.model.FolderModel;

public class BasicExplorerUI extends Composite implements HasText {

    private static ExploreUIUiBinder uiBinder = GWT
        .create(ExploreUIUiBinder.class);
    @UiField Tree tree;
    protected ExplorerBuilder builder;
    protected DataServiceWrapper serviceWrapper = DataServiceWrapper.getInstance();
    protected TreeManager treeManager;

    interface ExploreUIUiBinder extends UiBinder<Widget, BasicExplorerUI> {
    }

    public BasicExplorerUI() {
        initWidget(uiBinder.createAndBindUi(this));
        initialize();
    }

    private void initialize() {
       
        serviceWrapper.addDataServiceListener(new DataServiceListener() {

            public void onCreateRootFolder(FolderModel folder) {
                treeManager.addRootFolder(folder);
            }

            public void onCreateFolder(FolderModel folder, FolderModel parent) {
                treeManager.addSubFolder(parent.getKey(), folder);
            }

            public void onCreateData(DataModel data, FolderModel parent) {
                treeManager.addData(parent.getKey(), data);
            }

            public void onSelectChange(Object object, FolderModel folder) {
                // TODO Auto-generated method stub
                
            }

            public void onCreateChart(ChartModel chart, DataModel parent) {
               // treeManager.addChart(parent.getKey(), chart); 
            }

            public void onDelete(Key key) {
                if (!ChartModel.class.getName().contains(key.getKind())) {
                    treeManager.remove(key);
                }
            }

            public void onUpdateData(Key key) {
                treeManager.removeSub(key);
                builder.start(key);
            }

            public void onUpdatFolder(Key key) {
                // TODO Auto-generated method stub
                
            }
            
        });
        
        treeManager = new TreeManager(tree, new TreeManagerListener() {

            public void onSelectionChange(Key object) {
                serviceWrapper.setSelectedItem(object);
            }

        });

        builder = new ExplorerBuilder(new ExplorerBuilderListener() {

            public void addRootFolder(FolderModel folder) {
                treeManager.addRootFolder(folder);
            }

            public void addFolder(Key parent, FolderModel folder) {
                treeManager.addSubFolder(parent, folder);
            }

            public void addData(Key parent, DataModel data) {
                treeManager.addData(parent, data);
            }

            public void addChart(Key parent, ChartModel chart) {
                //treeManager.addChart(parent, chart);
            }
            
        });
        builder.start();

    }


    public void setText(String text) {
        
    }

    public String getText() {
        return "";
    }
    
    
    public void createFolder(String name, final Key parent) {
        serviceWrapper.createFolder(name, serviceWrapper.getSelectedFolder().getKey());
    }
    
    public void createRootFolder(String name) {
        serviceWrapper.createRootFolder(name);
    }

    protected void createData(String name, Key folderKey, String text) {
        serviceWrapper.createData(name, text, serviceWrapper.getSelectedFolder().getKey());

    }

    protected void updateData(DataModel data) {
        serviceWrapper.updateData(data);
    }
    
    public void rebuild() {
        tree.clear();
        builder.start();
    }
}

